package com.shinhye.wisdompeking.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.shinhye.wisdompeking.R;
import com.shinhye.wisdompeking.base.BaseFragment;
import com.shinhye.wisdompeking.base.BasePager;
import com.shinhye.wisdompeking.custom.LazyViewPager.OnPageChangeListener;
import com.shinhye.wisdompeking.custom.MyViewPager;
import com.shinhye.wisdompeking.pager.FunctionPager;
import com.shinhye.wisdompeking.pager.GovAffairsPager;
import com.shinhye.wisdompeking.pager.NewsCenterPager;
import com.shinhye.wisdompeking.pager.SettingPager;
import com.shinhye.wisdompeking.pager.SmartServicePager;

public class HomeFragment extends BaseFragment {
	@ViewInject(R.id.layout_content)
	private MyViewPager mvp_content;
	
	@ViewInject(R.id.main_radio)
	private RadioGroup main_radio;

	private View frag;

	private List<BasePager> pagerlist;

	@Override
	public View initView(LayoutInflater inflater) {
		frag = inflater.inflate(R.layout.frag_home, null);
		ViewUtils.inject(this, frag);
		return frag;
	}
	
	@Override
	public void initData(Bundle savedInstanceState) {
		pagerlist = new ArrayList<BasePager>();
		pagerlist.add(new FunctionPager(getActivity()));
		pagerlist.add(new NewsCenterPager(getActivity()));
		pagerlist.add(new SmartServicePager(getActivity()));
		pagerlist.add(new GovAffairsPager(getActivity()));
		pagerlist.add(new SettingPager(getActivity()));
		
		MyPagerAdapter adapter = new MyPagerAdapter();
		mvp_content.setAdapter(adapter);
		
		//设置页面改变时重新获取数据
		mvp_content.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				BasePager pager = pagerlist.get(position);
				pager.initData();
			}
			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
			}
			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
		//设置按钮点击后切换其他页面
		main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_function:
					mvp_content.setCurrentItem(0);
					break;
				case R.id.rb_news_center:
					mvp_content.setCurrentItem(1);
					break;
				case R.id.rb_smart_service:
					mvp_content.setCurrentItem(2);
					break;
				case R.id.rb_gov_affairs:
					mvp_content.setCurrentItem(3);
					break;
				case R.id.rb_setting:
					mvp_content.setCurrentItem(4);
					break;
				}
			}
		});
		
		//当前第一个radio button被选中
		main_radio.check(R.id.rb_function);
		//初始化第一页数据
		BasePager pager = pagerlist.get(0);
		pager.initData();
	}
	
	class MyPagerAdapter extends PagerAdapter {
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			BasePager pager = pagerlist.get(position);
			container.addView(pager.getRootView());
			return pager.getRootView();
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
		@Override
		public int getCount() {
			return pagerlist.size();
		}
		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}
	
	public NewsCenterPager getNewsCenterPager() {
		return (NewsCenterPager) pagerlist.get(1);
	}
}